﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Vision.Utilities.VisualStudioTool
{
    public partial class MainForm : Form
    {
        #region Properties
        private CodeChangeTrackForm codeChangeTrackForm = null;
        private KPIForm kpiForm = null;
        #endregion

        #region Ctor
        public MainForm()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void MainForm_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region events
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void codeChangeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!TFSAuthenticate())
                return;

            if (codeChangeTrackForm != null)
            {
                codeChangeTrackForm.Show();
            }
            else
            {
                codeChangeTrackForm = new CodeChangeTrackForm();
                codeChangeTrackForm.CloseButton = true;
                codeChangeTrackForm.Show(MainDockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document);
            }
        }
        #endregion

        #region
        private void kPIToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!TFSAuthenticate())
                return;

            if (kpiForm != null)
            {
                kpiForm.Show();
            }
            else
            {
                kpiForm = new KPIForm();
                kpiForm.CloseButton = true;
                kpiForm.Show(MainDockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document);
            }
        }
        #endregion

        #region About
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Welcome to use Visual Studio Tool.");
        }
        #endregion

        #region Authetication
        private bool TFSAuthenticate()
        {
            if (!Util.AutoAuthenticate())
            {
                var tfsAuthenticationForm = new TFSAuthenticationForm();
                tfsAuthenticationForm.StartPosition = FormStartPosition.CenterParent;
                var dialogResult = tfsAuthenticationForm.ShowDialog();
                var result = Util.AuthenticatedTfsTeamProjectCollection != null;
                if (!result)
                    MessageBox.Show("Sorry, Login Failed.");

                return result;
            }
            return true;
        }
        #endregion
    }
}
